﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;

namespace iTunesPlaycountUpdater
{
    class iTunesLibary
    {
        private List<iTunesArtist> artists = new List<iTunesArtist>();

        public IITTrack GetTrack(string artist, string album, string trackName)
        {
            iTunesArtist artistRef = artists.Find(a => a.Name == artist);
            if (artistRef != null)
            {
                iTunesAlbum albumRef = artistRef.GetAlbum(album);
                if (albumRef != null)
                {
                    iTunesTrack trackRef = albumRef.GetTrack(trackName);
                    if (trackRef != null)
                    {
                        return trackRef.iTunesTrackRef;
                    }
                }
            }
            return null;
        }
        public void AddTrack(IITTrack track)
        {
            iTunesArtist artist = GetMember(ref artists, track.Artist) ?? new iTunesArtist(track.Artist);
            iTunesAlbum album = GetMember(ref artist.albums, track.Album) ?? new iTunesAlbum(track.Album);
            album.AddTrack(new iTunesTrack(track));
            artist.AddAlbum(album);
            artists.Add(artist);
        }

        private T GetMember<T>(ref List<T> members, string match) where T : iTunesLibraryMember
        {
            T member = null;
            int index = members.FindIndex(m => m.Name == match);
            if (index >= 0)
            {
                member = members[index];
                members.RemoveAt(index);
            }
            return member;
        }

        private abstract class iTunesLibraryMember
        {
            public abstract string Name { get; }
        }
        private class iTunesArtist : iTunesLibraryMember
        {
            private readonly string name;
            public List<iTunesAlbum> albums;
            public override string Name { get { return name; } }

            public iTunesArtist(string name)
            {
                this.name = name;
                this.albums = new List<iTunesAlbum>();
            }

            public iTunesAlbum GetAlbum(string name)
            {
                return albums.Find(a => a.Name == name);
            }
            public void AddAlbum(iTunesAlbum album)
            {
                if (albums != null)
                    if (!albums.Contains(album))
                        albums.Add(album);
            }
            public bool ContainsAlbum(string name)
            {
                return albums.FindAll(a => a.Name == name).Count > 0;
            }
        }
        private class iTunesAlbum : iTunesLibraryMember
        {
            private readonly string name;
            public List<iTunesTrack> tracks;
            public override string Name { get { return name; } }

            public iTunesAlbum(string name)
            {
                this.name = name;
                this.tracks = new List<iTunesTrack>();
            }

            public iTunesTrack GetTrack(string name)
            {
                return tracks.Find(t => t.Name == name);
            }
            public void AddTrack(iTunesTrack track)
            {
                if (track != null)
                    if (!tracks.Contains(track))
                        tracks.Add(track);
            }
            public bool ContainsTrack(string name)
            {
                return tracks.FindAll(t => t.Name == name).Count > 0;
            }
        }
        private class iTunesTrack : iTunesLibraryMember
        {
            private readonly string name;
            public override string Name { get { return name; } }
            public IITTrack iTunesTrackRef { get; private set; }

            public iTunesTrack(IITTrack track)
            {
                this.name = track.Name;
                this.iTunesTrackRef = track;
            }
        }
    }
}
